package lib_model.Factories;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import lib_dal.Connection.ConnectMSSQL;
import lib_dal.Interface.ISql;
import lib_model.Orther.ReturnObj;

public class Stocks_factory implements ISql {
	public int factory_id_ref;
	public int products_id_ref;
	public int quantity;
	public Date date_create;
	public Date date_update;

	public Stocks_factory() {
	}

	@Override
	public ReturnObj Select() {
		String sql = "select factory_id_ref, products_id_ref, quantity, date_create, date_update from dbo.stocks_factory";
		ReturnObj obj = new ReturnObj();
		ArrayList<Stocks_factory> arr = new ArrayList<Stocks_factory>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Stocks_factory a = new Stocks_factory();
				a.factory_id_ref = rs.getInt("factory_id_ref");
				a.products_id_ref = rs.getInt("products_id_ref");
				a.quantity = rs.getInt("quantity");
				a.date_create = rs.getDate("date_create");
				a.date_update = rs.getDate("date_update");
				arr.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}
	
	public ReturnObj SelectByIDfactory(int id_fac) {
		String sql = "select factory_id_ref, products_id_ref, quantity, date_create, date_update from dbo.stocks_factory where factory_id_ref = " + id_fac;
		ReturnObj obj = new ReturnObj();
		ArrayList<Stocks_factory> arr = new ArrayList<Stocks_factory>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Stocks_factory a = new Stocks_factory();
				a.factory_id_ref = rs.getInt("factory_id_ref");
				a.products_id_ref = rs.getInt("products_id_ref");
				a.quantity = rs.getInt("quantity");
				a.date_create = rs.getDate("date_create");
				a.date_update = rs.getDate("date_update");
				arr.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}

	@Override
	public ReturnObj Add() {
		String sql = "insert into dbo.stocks_factory  (factory_id_ref,   products_id_ref,   quantity,   date_create) values (?,   ?,   ?,   GETDATE())";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.factory_id_ref);
			preState.setInt(2, this.products_id_ref);
			preState.setInt(3, this.quantity);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Update() {
		String sql = "update dbo.stocks_factory set quantity = ?,   date_update = GETDATE() where  factory_id_ref = ? and products_id_ref = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.quantity);
			preState.setInt(2, this.factory_id_ref);
			preState.setInt(3, this.products_id_ref);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Delete() {
		String sql = "delete from dbo.stocks_factory where factory_id_ref = ? and products_id_ref = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.factory_id_ref);
			preState.setInt(2, this.products_id_ref);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj GetInformation() {
		ReturnObj obj = new ReturnObj();
		String sql = "select factory_id_ref, products_id_ref, quantity, date_create, date_update from dbo.stocks_factory where factory_id_ref = ? and products_id_ref = ?";
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.factory_id_ref);
			preState.setInt(2, this.products_id_ref);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Stocks_factory a = new Stocks_factory();
				a.factory_id_ref = rs.getInt("factory_id_ref");
				a.products_id_ref = rs.getInt("products_id_ref");
				a.quantity = rs.getInt("quantity");
				a.date_create = rs.getDate("date_create");
				a.date_update = rs.getDate("date_update");
				obj.ObjResult = a;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

}
